﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.BLL;
using Grove.ORM;
using HZ.MBSM.Model;
using HZ.MBSM.DBUtility;
using System.Drawing;
public partial class Bidder_GoodsList : System.Web.UI.Page, IPageDescribe
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                if (!IsPostBack)
                {
                    List_memo3.SelectedIndex = 4;
                }



                //if (Request.Form["SubDel"] == "删除商品")
                //{
                //    String _strdel = Request.Form["txtDel"];
                //    Db db = new Db();
                //    String Sql = "delete from goods Where Goodsid in (" + _strdel + ") and Checkstatus=0 and account_tjr=" + _Account.AccountId.ToString();
                //    db.Command(Sql);
                //    db.Close();
                //    // HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                //    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "删除", "goods", Sql, "../Bidder/GoodsList.aspx");

                //}

                //if (Request.Form["SubRecover"] == "恢复商品")
                //{
                //    String _strRecover = Request.Form["txtDel"];
                //    Db db = new Db();
                //    String Sql = "Update Goods SET IsDel='1' Where Goodsid in (" + _strRecover + ")";
                //    db.Command(Sql);
                //    db.Close();
                //    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "goods", Sql, "../Bidder/GoodsList.aspx");

                //}
                //string sort = "";
                //sort = _Account.Sort;
                //if (sort.Equals("0"))
                //{
                showGridView();
                //}
                //else
                //{
                //    Response.Write("<script>window.alert('此功能对经营企业不适用！');</script>");
                //    Response.Write("<script>window.location.href='CompanyInfo.aspx'</script>");

                //}
            }

        }
    }

    public void showGridView()
    {

        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "GoodsByCompany";                                               //要查询的表明
        dbPage.Key = "GoodsByCompanyId";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "convert(varchar(100),shenhetime,23) shenhetime,GoodsByCompanyId,ProductName,Outlookc,Medicinemodel,Unit,xishu,NoPass,case CheckStatus when '1' then '审核通过' when '2' then '审核未通过' when '3' then '纸质材料一致' else '未审核' end as CheckStatus, CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels ";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 10;                                                    //没有显示记录条数   
        dbPage.OrderBy = "productname,medicinemodel,outlookc,GoodsByCompanyId";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        //dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        //08.18
        dr = null;
        db = null;

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

    }

    public string fctConditions()
    {
        Int64 Account_tjr;
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];

        string _strWhere = "";
        if (_Account != null)
        {
            Account_tjr = _Account.AccountId;

            _strWhere = @"and  Accountid_tjr=" + Account_tjr+" ";

            //药品通用名或拼音码

            string _strProductName = SysFun.ToTrim(txtProductName.Text);
            if (_strProductName != "")
            {
                _strWhere += "and  (ProductName like '%" + _strProductName + "%' ) ";
            } 
            //审核状态
            if (List_memo3.SelectedValue != "4")
            {
                _strWhere += "and isnull(GoodsByCompany.CheckStatus,0)='" + List_memo3.SelectedValue + "' ";
            }

            //txtMedicinemodel
            //剂型
            string _strMedicinemodel = SysFun.ToTrim(txtMedicinemodel.Text);
            if (_strMedicinemodel != "")
            {
                _strWhere += "and  Medicinemodel like '%" + _strMedicinemodel + "%' ";
            }

            //规格
            string _strOutlookc = SysFun.ToTrim(txtOutlookc.Text);
            if (_strOutlookc != "")
            {
                _strWhere += " and Outlookc like '%" + _strOutlookc + "%'";
            }
            ////品目号 采用productid作为品目号显示在商品列表中 edit by ws 2008.9.26

            //string _strProductCode = SysFun.ToTrim(txtProductCode.Text);
            //if (_strProductCode != "")
            //{
            //    _strWhere += " and Product.Productid like '%" + _strProductCode + "%'";
            //}

            //流水号

            string _strGoodsId = SysFun.ToTrim(txtGoodsId.Text);
            if (_strGoodsId != "")
            {
                _strWhere += " And GoodsByCompanyId =" + _strGoodsId + " ";
            }



            ////提交状态

            //string _strMemo3 = lstMemo3.SelectedValue;
            //switch (_strMemo3)
            //{
            //    case "全部":
            //        break;
            //    case "未保存":
            //        _strWhere += " and goods.Memo3 is null  ";
            //        break;
            //    case "已保存未提交":
            //        _strWhere += " and goods.Memo3=0  ";
            //        break;
            //    case "已提交":
            //        _strWhere += " and goods.Memo3=1 ";
            //        break;
            //    case "已修改":
            //        _strWhere += " and goods.Memo3=2 ";
            //        break;
            //}

            //string _strCheckStatus = DDLCheckStatus.SelectedValue.ToString();
            //switch (_strCheckStatus)
            //{
            //    case "全部":

            //        break;
            //    case "未审核":
            //        _strWhere += " And goods.checkstatus=0";
            //        break;
            //    case "审核通过":
            //        _strWhere += " And goods.checkstatus=1";
            //        break;
            //    case "审核未通过":
            //        _strWhere += " And goods.checkstatus=2";
            //        break;
            //}
        }
        if (_strWhere != "")
        {
            _strWhere = "  " + _strWhere.Remove(0, 3);
        }

        return _strWhere;
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[7].Text == "审核未通过")//审核状态
            {
                e.Row.BackColor = Color.Yellow;
            }
        }



    }

    //public void Button2show()
    //{
    //    HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
    //    string str_sql = "select count(1) from v_product_goods a left join bid_project_product b on a.productid=b.productid where a.checkstatus=1 and a.companycheck=1 and b.projectid=13 and Accountid_sc=" + _Account.AccountId;
    //    Db db = new Db();
    //    DataTable gg = db.DataTable(str_sql);
    //    if (gg != null && gg.Rows[0][0].Equals(0) == false)
    //    {
    //        Button2.Visible = true;
    //    }
    //    db.Close();
    //    gg.Dispose();

    //    //这里你只限定了goods.checkstatus =1 但审查通过的商品里也有可能是自行采购的,此时就无需再维护投标人,只有招标目录的商品审核通过才显示
    //    //而且生产企业本身未通过审核则也无维护投标人的必要
    //    //v_product_goods视图已经有你写的这个连接查询可以直接使用
    //    //用count(1)查询总行数的效率比count(*)快的多;
    //    //string str_sql = " select count(*) from  product inner join goods on product.productid=goods.productid inner join company on goods.Accountid_sc=company.accountid where goods.checkstatus =1 and Accountid_sc=" + _Account.AccountId;
    //    // int _check ;
    //    //Db db = new Db();
    //    //DataTable gg = db.DataTable(str_sql);
    //    //_check =Convert.ToInt32(gg.Rows[0][0].ToString().Trim());
    //    //if (_check == 0)
    //    //{
    //    //    Button2.Visible = false;//默认button为false 只有当查询到值的时候才执行,所以无需执行Button2.Visible = false;
    //    //}
    //    //else
    //    //{ Button2.Visible = true; }
    //    //db.Close();
    //    //gg.Dispose();      



    //}

    #region IPageDescribe 成员


    public string Describe
    {
        get { return ""; }
    }
    public string Name
    {
        get { return "本企业商品信息"; }
    }
    public string ParentName
    {
        get { return "商品维护"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumComapanyRG);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);

            return _AccountType;
        }
    }

    #endregion

 

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "未审核")
        {
            HZ.MBSM.BLL.Goods bllgoods = new HZ.MBSM.BLL.Goods();
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (bllgoods.GoodsByCompanyDelete(Convert.ToInt32(e.CommandArgument)))
            {
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "删除", "GoodsByCompany", "bllgoods.GoodsByCompanyDelete(GoodsByCompanyId)" + e.CommandArgument, "../Company/GoodsList.aspx");

                showGridView();
                Response.Write("<script>alert('删除成功！！！');</script>");

            }
            else
            {
                Response.Write("<script>alert('发生异常！！！');</script>");
            }
        }
        else if (e.CommandName == "审核通过")
        {
            Response.Write("<script>alert('审核通过品种不让删除！！！');</script>");
        }
        else if (e.CommandName == "纸质材料一致")
        {
            Response.Write("<script>alert('纸质材料一致品种不让删除！！！');</script>");
        }
        else if (e.CommandName == "审核未通过")
        {
            Response.Write("<script>alert('审核未通过品种不让删除！！！');</script>");
        }
    }
}
